home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 393 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  62 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: pointer conversions from the null pointer
  5. Date: 20 Feb 1996 21:32:36 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4gddco$cqv@engnews1.Eng.Sun.COM>
  9. References: <baluDn1pGL.8J1@netcom.com>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. X-Nntp-Posting-Host: taumet.eng.sun.com
  14. Content-Length: 928
  15. X-Lines: 37
  16. Originator: clamage@taumet
  17.  
  18. In article 8J1@netcom.com, balu@netcom.com (Balasubramanian) writes:
  19. >Given the following:
  20.  
  21. >class A {};
  22. >class B : public A {};
  23. >class C : public B {};
  24.  
  25. >void foo(A*) { ... }
  26.  
  27. >void foo(C*) { ... }
  28.  
  29. >int main() {foo(0); return 0;}
  30.  
  31. >How is the call to foo() in main() to be resolved? It seemed obvious
  32. >to me that the call should be ambiguous ...
  33.  
  34. It seems obvious to me too. The null pointer constant is an exact match
  35. for any pointer, so there is no reason to prefer either version of foo.
  36.  
  37. There is a preference associated with class hierarchies. For example:
  38.     class X { ... };
  39.     class Y : public X { ... };
  40.     class Z : public Y { ... };
  41.     void foo(X*);
  42.     void foo(Y*);
  43.  
  44.     Z* pz = ...;
  45.     foo(pz);
  46.  
  47. A Z* can be converted to either a Y* or an X*, but Y* is preferred. This
  48. case does not apply to null pointer constants, however, since a null
  49. pointer constant does not have type Z*.
  50.  
  51. ---
  52. Steve Clamage, stephen.clamage@eng.sun.com
  53.  
  54.  
  55.  
  56. [ To submit articles: Try just posting with your newsreader.
  57.               If that fails, use mailto:std-c++@ncar.ucar.edu
  58.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  59.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  60.   Comments? mailto:std-c++-request@ncar.ucar.edu
  61. ]
  62.